package edu.infomatik.tundra.data;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import edu.infomatik.tundra.util.TundraConstants;

public class DAOtundra implements ITundraData<IBean>{
	private static DAOtundra instance=null;
	private static long maxId=-1;
	private DAOtundra() {}
	public static DAOtundra getInstance(){
		Session session = DataUtils.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Query query = session.createQuery("select max(id) from edu.infomatik.tundra.logic.WorkBean ");
		maxId = (Long)query.iterate().next();
		session.createSQLQuery("ALTER TABLE `work` AUTO_INCREMENT="+(maxId+1)).executeUpdate();

		session.getTransaction().commit();
		if (null==instance) instance=new DAOtundra();
		return instance;
	}
	public long getMaxId() {
		return maxId;
	}
	

	@Override
	public List<IBean> getAllBeans(IBean b) {
		return getAllBeans(b, "");
	}
	
	@SuppressWarnings("unchecked")
	private List<IBean> getAllBeans(IBean b, String query) {
		if (null==query) query="";
		Session session = DataUtils.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<IBean> result = null;
		StringBuffer resultQRY = new StringBuffer();
		switch (b.getClassId()){
			case TundraConstants.WORK_CLASS:
				resultQRY.append("from edu.infomatik.tundra.logic.WorkBean ");
				break;
			case TundraConstants.SPRINT_CLASS:
				resultQRY.append("from edu.infomatik.tundra.logic.SprintBean ");
				break;
			case TundraConstants.USER_STORY_CLASS:
				resultQRY.append("from edu.infomatik.tundra.logic.UStoryBean ");
				break;
			case TundraConstants.USER_TASK_CLASS:
				resultQRY.append("from edu.infomatik.tundra.logic.USTaskBean ");
				break;
			case TundraConstants.SYSTEM_ACCESS_BEAN:
				resultQRY.append("from edu.infomatik.tundra.logic.security.SystemAccessBean ");
				break;
			default:
				resultQRY.append("from edu.infomatik.tundra.logic.WorkBean ");		
			}
			if (query.length()>0) resultQRY.append(query);	
	
			result=session.createQuery(resultQRY.toString()).list();
		
		session.getTransaction().commit();
		return result;	
	}

//	@Override
//	public IBean getBeanById(long id) {
//		return getBeanById(new WorkBean(), id);
//	}

	@Override
	public IBean getBeanById(IBean b) {
		List<IBean> resutList=getAllBeans(b, "where id = "+ b.getId());
		if (!resutList.isEmpty()) return resutList.get(0);
		return null;
	}

	@Override
	public List<IBean> getRange(IBean b, int indexFrom, int indexTo) {
		return getAllBeans(b, "where id between "+ indexFrom + " and "+ indexTo);
	}
	
	@Override
	public void delete(IBean b){
		Session session = DataUtils.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		switch (b.getClassId()){
			case TundraConstants.WORK_CLASS:
				session.createSQLQuery("DELETE FROM `work` WHERE projectId="+b.getId()).executeUpdate();
				break;
			case TundraConstants.SPRINT_CLASS:
				session.createSQLQuery("DELETE FROM `ustask` WHERE storyId IN ("+b.getId()+")").executeUpdate();
				session.createSQLQuery("DELETE FROM `ustory` WHERE sprintId="+b.getId()).executeUpdate();
				break;
			case TundraConstants.USER_STORY_CLASS:
				session.createSQLQuery("UPDATE `ustory` SET linkedWith=-1 WHERE linkedWith="+b.getId()).executeUpdate();				
				session.createSQLQuery("DELETE FROM `ustask` WHERE storyId="+b.getId()).executeUpdate();
				break;
			case TundraConstants.USER_TASK_CLASS:
				break;
			case TundraConstants.SYSTEM_ACCESS_BEAN:
				break;
		}
		session.delete(b);
		session.getTransaction().commit();
	}
		
	@Override
	public void update(IBean b){
		Session session = DataUtils.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		session.merge(b);
		
		session.getTransaction().commit();		
	}
}
